﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DataBaseLibrary.DBInterfaces;
using DataBaseLibrary.DBObjectsFB;

namespace DocMreoLiteVersion.Controls
{


    /// <summary>
    /// Interaction logic for IndividualControl.xaml
    /// </summary>
    public partial class IndividualControl : UserControl
    {

        public static readonly DependencyProperty HeaderWindowProperty =
            DependencyProperty.Register("HeaderWindow", typeof(String), typeof(IndividualControl));


        /// <summary>
        /// Заголовок окна компонента
        /// </summary>
        public String HeaderWindow
        {
            set { DataContext = value; SetValue(HeaderWindowProperty, value); }
            get { return (String)GetValue(HeaderWindowProperty); }
        }
        
        
        /// <summary>
        /// Объект данных о ф.л.
        /// </summary>
        private Individual mIndividualObject;

        public Individual IndividualObject
        {
            set
            {
                DataContext = value;
                mIndividualObject = value;
            }
            get { return mIndividualObject; }
        }


        /// <summary>
        /// Объект провайдера БД
        /// </summary>
        public IDBProvider Provider { set; get; }


        public IndividualControl()
        {
            InitializeComponent();                        
        }


        private void LoadCitys(String region)
        {
            CityComboBox.Items.Clear();
            if (!CSettings.RegionsCityDictionary.ContainsKey(region))
                return;
            foreach (string city  in CSettings.RegionsCityDictionary[region])
            {
                CityComboBox.Items.Add(city);
            }
            CityComboBox.SelectedIndex = 0;
        }


        public void SearchIndividual()
        {
            if (Provider == null)
            {
                MessageBox.Show("Ошибка доступа: не найден провайдер БД.");
                return;
            }
            
            FDBLogic logic = new FDBLogic(Provider);
            IDBLogic.ResultFields result = logic.SearchIndividualsFio(IndividualObject);
            SearchResultTable resultTableDlg = new SearchResultTable(result);
            resultTableDlg.Owner = System.Windows.Application.Current.MainWindow;
            if (resultTableDlg.ShowDialog() ?? true)
            {
                Provider.Load(IndividualObject, resultTableDlg.ReturnObject);
            }

        }
        

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                const FocusNavigationDirection focusNav = FocusNavigationDirection.Next;
                var request = new TraversalRequest(focusNav);

                //ComboBox <Enter>
                if ((sender as ComboBox) != null && (sender as ComboBox).IsEditable)
                {

                    if (sender.Equals(RegionComboBox)) 
                        LoadCitys((sender as ComboBox).Text);

                    (sender as ComboBox).IsEditable = false;
                    (sender as UIElement).MoveFocus(request);
                    (sender as ComboBox).IsEditable = true;

                    e.Handled = true;
                    return;                    
                }

                if ((sender as UIElement) != null)
                    (sender as UIElement).MoveFocus(request);



                e.Handled = true;
            }

        }

        private void IndividualControl_OnLoaded(object sender, RoutedEventArgs e)
        {
            RegionComboBox.Items.Clear();
            foreach (var region in CSettings.RegionsCityDictionary.Keys)
            {
                RegionComboBox.Items.Add(region);
            }
            
        }

        private void RegionComboBox_OnSelectionChanged(object sender, RoutedEventArgs routedEventArgs)
        {
            int index = (sender as ComboBox).SelectedIndex;
            if (index < 0 || index >= (sender as ComboBox).Items.Count)
                return;

            if (sender.Equals(RegionComboBox))
                LoadCitys((sender as ComboBox).Items[index].ToString());
        }

        /// <summary>
        /// Поиск 
        /// </summary>
        private void SearchButtonClick(object sender, RoutedEventArgs e)
        {
            SearchIndividual();
        }
    }
}
